<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class AppSession {
    var $id;
    var $app_session_id;
    var $remote_address;
    var $login_time;
    var $logout_time;
    var $app_user_id;
    var $user_agent;
    //var $Users=new AppUsers();

    function open($id){
        global $db;
        $sql="select * from app_session where id=$id";
        $row=$db->fetch_row($sql);
        if (!empty($row)){
            extract($row);
            $this->app_session_id=$app_session_id;
            $this->remote_address=$remote_address;
            $this->login_time=$login_time;
            $this->logout_time=$logout_time;
            $this->app_user_id=$app_user_id;
            $this->user_agent=$user_agent;
        }
    }
    function openSession($session_id){
        global $db;
        $sql="select * from app_session where app_session_id='$app_session_id'";
        $row=$db->fetch_row($sql);
        if (!empty($row)){
            extract($row);
            $this->app_session_id=$app_session_id;
            $this->remote_address=$remote_address;
            $this->login_time=$login_time;
            $this->logout_time=$logout_time;
            $this->app_user_id=$app_user_id;
            $this->user_agent=$user_agent;
            $Users=new AppUsers();
            $Users->open($this->app_session_id);
            
            return $Users;
        } else return NULL;
    }
    function openAll(){
        $sql="select * from app_session order by id asc";
        $result=$db->query($sql);
        return $db->fetch_array($result);
        
    }
    function is_exist($app_user_id,$app_session_id){
        global $db;
        $sql="select id from app_session 
            where app_session_id='$app_session_id' and app_user_id=$app_user_id";
        $row=$db->fetch_row($sql);
        if (!empty($row)){
            
            return $row['id'];
            
        } else return 0;
    }
    function updateLogin(){
    global $db;
    if ($this->is_exist($this->app_user_id, $this->app_session_id)==0){
    $sql="insert into app_session values (null,'{$this->app_session_id}',
        '{$this->remote_address}',sysdate(),
            sysdate(),'{$this->app_user_id}','{$this->user_agent}')";
    $db->query($sql);
    } else {
    $sql="update app_session set login_time=sysdate()
          where  app_session_id='{$this->app_session_id}' and app_user_id={$this->app_user_id}";
    $db->query($sql);    
    }
    $sql="update app_users set active='Y',active_session='{$this->app_session_id}' 
        where id={$this->app_user_id}";
    $db->query($sql);
    }
    function updateLogout(){
        global $db;
        $sql="update app_session set
        logout_time=sysdate() where 
        app_session_id='{$this->app_session_id}' and app_user_id={$this->app_user_id}";
        $db->query($sql);
        $sql="update app_users set active='N' where id={$this->app_user_id}";
        $db->query($sql);
    }
    function insert(){
    global $db;
    $sql="insert into app_session values (null,'{$this->app_session_id}',
        '{$this->remote_address}','{$this->login_time}',
            '{$this->logout_time}','{$this->app_user_id}')";
    $db->query($sql);
    return mysql_insert_id();
    }

    function update(){
        global $db;
        $sql="update app_session set app_session_id='{$this->app_session_id}',
remote_address='{$this->remote_address}',login_time='{$this->login_time}',
logout_time='{$this->logout_time}',
app_user_id='{$this->app_user_id}'  where id={$this->id}";
        $db->query($sql);
    }

    function delete(){
        global $db;
        $sql="delete from app_session where id=".$this->id;
        $db->query($sql);
    }
}
?>
